﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using System.Web.UI.HtmlControls;

namespace WebSys.UI.Control.ZhengShou
{
    public class JDSelectControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack) {
                BindData();
            }
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            CustomGridList1.OnCancelClick += new EventHandler(CustomGridList1_OnCancelClick);
            CustomGridList1.OnComplelteClick += new EventHandler<CustomGridViewEventArgs>(CustomGridList1_OnComplelteClick);
            Button bt_query = (Button)FindControl("btnSearch");
            bt_query.Click += new EventHandler(bt_query_Click);
        }

        protected void bt_query_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void BindData() {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridView1");
            string strpnid = base.Page.Request["PID"];
            DataTable dt = _EluZSDataProvider.GetBCJDXZRY(strpnid, GetSqlWhere());
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.HideColName = "ID";
            CustomGridList1.DataKeyNames = new string[] { "ID" };
            CustomGridList1.DataBind();
        }
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            BindData();
        }     
        protected string GetSqlWhere() {
            string sqlWhere = string.Empty;
            TextBox txtCQR = (TextBox)FindControl("txtCQR");
            TextBox txtCZR = (TextBox)FindControl("txtCZR");
            TextBox txtFWZL = (TextBox)FindControl("txtFWZL");
            if (!string.IsNullOrEmpty(txtCQR.Text))
            {
                sqlWhere += " and 产权人 like '%" + txtCQR.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtCZR.Text))
            {
                sqlWhere += " and 承租人 like '%" + txtCZR.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtFWZL.Text))
            {
                sqlWhere += " and 房屋坐落 like '%" + txtFWZL.Text + "%'";
            }
            return sqlWhere;
        }

        protected void CustomGridList1_OnCancelClick(object sender, EventArgs e)
        {
            base.Page.Response.Write("<script>window.close();</script>");
        }
        protected void CustomGridList1_OnComplelteClick(object sender, CustomGridViewEventArgs e)
        {
            if (e == null)
            {
                BindData();
                Label Label1 = (Label)FindControl("Label1");
                Label1.Text = "请勾选人员！";
                return;
            }
            ProcesOnlyValue(e.MultiSelectKeys);
        }
        private void ProcesOnlyValue(DataKey[] keys)
        {
            string values = "";
            for (int i = 0; i < keys.Length; i++)
            {
                values += keys[i].Value.ToString() + "|";
            }
            if (values.Length > 0)
            {
                values = values.Remove(values.Length - 1);
            }
            InsertVas(values);
        }
        protected void InsertVas(string values)
        {
            Label Label1 = (Label)FindControl("Label1");
            StringBuilder myblr = new StringBuilder();
            string PID = base.Page.Request["PID"].Trim();
            string[] temp = values.Split('|');
            DataTable dt = _EluZSDataProvider.GetXZBCJDRY(PID, values,"0");
            if (dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    string strZJH = dt.Rows[j]["证件号"].ToString();
                    string strCQR = dt.Rows[j]["产权人"].ToString();
                    if (myblr.Length > 1)
                    {
                        myblr.Append("&nbsp;");
                    }
                    myblr.Append(strZJH);
                    myblr.Append("-");
                    myblr.Append(strCQR);
                }
                Label1.Text = "本项目中以下人员数据已经存在：" + myblr + "";
            }
            else
            {
                DataTable dt1 = _EluZSDataProvider.GetXZBCJDRY(PID, values, "1");
                if (dt1.Rows.Count > 0)
                {
                    if (temp.Length > 0)
                    {
                        for (int i = 0; i < temp.Length; i++)
                        {
                            string PERSONID = temp[i];
                            EluZSProvider.BCJD bcjd = new EluZSProvider.BCJD();
                            bcjd.PNID = PID;
                            bcjd.PERSONID = PERSONID;
                            bcjd.ID = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper();
                            bool save = _EluZSDataProvider.SaveBCJD("", bcjd);
                        }
                    }
                    Label1.Text = "";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "selecthouse", string.Format("<script>setParentValue('{0}');</script>", values));
                }
                else
                {
                    Label1.Text = "在该项目下选择的人员在住宅补偿金分户不存在数据！";
                }

            }
        }
    }
}


